package com.refusea.train.config.mvc

import com.refusea.train.domain.entity.User
import com.refusea.train.domain.ex.BizEx

object WebSession {

    private val userThreadLocal = ThreadLocal<User>()
    private val tokenThreadLocal = ThreadLocal<String>()

    fun hold(token: String, user: User) {
        tokenThreadLocal.set(token)
        userThreadLocal.set(user)
    }

    fun user(): User {
        return userThreadLocal.get() ?: throw BizEx.of("用户未登录或登录已过期")
    }

    fun token(): String {
        return tokenThreadLocal.get() ?: throw BizEx.of("用户未登录或登录已过期")
    }

    fun clear() {
        tokenThreadLocal.remove()
        userThreadLocal.remove()
    }
}
